package com.youcheng.controller;

import com.youcheng.constant.ErrorTips;
import com.youcheng.dao.Admin;
import com.youcheng.service.admin.AdminService;
import com.youcheng.utils.ApiResponse;
import com.youcheng.utils.Constants;
import com.youcheng.utils.Helper;
import com.youcheng.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"管理员相关接口文档"})
@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisUtil redisUtil;

    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    @ApiOperation(value = "管理员登录")
    @PostMapping("/admin/login")
    @ResponseBody
    public ApiResponse adminLogin(
            @ApiParam(name = "username", value = "用户名", required = true)
            @RequestParam String username,

            @ApiParam(name = "password", value = "密码", required = true)
            @RequestParam String password,
            HttpServletRequest request
    ){
        try {
            logger.info("info");
            logger.debug("debug");
            logger.warn("warn");
            logger.error("error");
            Admin admin = adminService.getByUsername(username,password);
            //存储管理员信息到redis 有效期 7200秒
            String key = Constants.TOKEN_PREFIX +admin.getToken();
            redisUtil.set(key , admin , 7200L);
            Map<String, Object> map = new HashMap<>();
            map.put("token",admin.getToken());
            map.put("token_expire",admin.getToken_expire());
            map.put("username",admin.getUsername());
            return ApiResponse.success(map);
        }catch (Exception e){
            return ApiResponse.error(ErrorTips.commonErrorCode,e.getMessage());
        }
    }

    @ApiOperation(value = "新增管理员")
    @PostMapping("/admin/add")
    @ResponseBody
    public ApiResponse addAdmin(
            @ApiParam(name = "username", value = "用户名", required = true)
            @RequestParam String username,

            @ApiParam(name = "password", value = "密码", required = true)
            @RequestParam String password
    ){
        if(username ==null || username.isEmpty()){
            return ApiResponse.error("用户名不能为空");
        }
        if(password ==null || password.isEmpty()){
            return ApiResponse.error("密码不能为空");
        }
        Admin admin = new Admin(username,password, Helper.getTimeSecond());
        try {
            adminService.addAdmin(admin);
            Map<String, Object> map = new HashMap<>();
            map.put("id",admin.getId());
            return ApiResponse.success(map);
        }catch (Exception e){
            return ApiResponse.error(ErrorTips.commonErrorCode,e.getMessage());
        }
    }

    @ApiOperation(value = "锁定管理员")
    @PostMapping("/admin/lock")
    @ResponseBody
    public ApiResponse lockAdmin(
            @ApiParam(name = "id", value = "管理员ID", required = true)
            @RequestParam int id
    ){
        try {
            adminService.lockAdmin(id, 1);
        }catch (Exception e){
            return ApiResponse.error(ErrorTips.commonErrorCode,e.getMessage());
        }
        return ApiResponse.success();
    }

    @ApiOperation(value = "解锁管理员")
    @PostMapping("/admin/unlock")
    @ResponseBody
    public ApiResponse unLockAdmin(
            @ApiParam(name = "id", value = "管理员ID", required = true)
            @RequestParam int id
    ){
        try {
            adminService.lockAdmin(id, 0);
        }catch (Exception e){
            return ApiResponse.error(ErrorTips.commonErrorCode,e.getMessage());
        }
        return ApiResponse.success();
    }

    @ApiOperation(value = "管理员列表")
    @PostMapping("/admin/list")
    @ResponseBody
    public ApiResponse listAdmin(
            @ApiParam(name = "username", value = "用户名")
            @RequestParam(defaultValue = "") String username,

            @ApiParam(name = "is_lock", value = "状态")
            @RequestParam(defaultValue = "-1") int is_lock
    ){
        List<Admin> admins = adminService.listAllAdmin(username,is_lock);
        List<Map> list = new ArrayList<>();
        for (Admin admin : admins) {
            Map<String, Object> map = new HashMap<>();
            map.put("id",admin.getId());
            map.put("username",admin.getUsername());
            map.put("is_lock",admin.getIs_lock());
            map.put("create_at",Helper.timestamp2Date( admin.getCreate_at(), "yyyy-mm-dd HH:mm:ss"));
            map.put("login_times",admin.getLogin_times());
            list.add(map);
        }
        return ApiResponse.success(list);
    }

}
